package com.eascs.web.captcha.interceptor;

import javax.inject.Inject;

import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;

import com.eascs.app.context.ConfigContext;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.frequency.service.FrequencyService;
import com.eascs.site.session.SessionSpec;
import com.eascs.web.captcha.controller.CaptchaBuilder;
import com.eascs.web.context.RequestContext;
import com.eascs.web.interceptor.AnnotationProcessor;

public class CaptchaAnnoationProcessor implements AnnotationProcessor<CaptchaRequired> {
	private final Logger LOGGER = LoggerFactory.getLogger(CaptchaAnnoationProcessor.class);
	private final SessionSpec<String> SESSION_USER_ID = new SessionSpec<String>("userId");
	@Inject
	private FrequencyService frequencyService;

	public boolean checkCaptchaStatus() {
		String reqCaptcha = RequestContext.getParameter("captcha");
		String token = CaptchaBuilder.SESSION_CAPTCHA.getValue();
		CaptchaBuilder.SESSION_CAPTCHA.setValue("");
		if (!StringUtils.isEmpty(reqCaptcha) && !StringUtils.isEmpty(token)) {
			if (reqCaptcha.equalsIgnoreCase(token)) {
				return true;
			}
		}
		// 内部测试环境所使用的验证码
		String configValue = ConfigContext.getStringValue("web.intranet.captcha", null);
		if (StringUtils.hasText(configValue) && configValue.equalsIgnoreCase(reqCaptcha) && RequestContext.isPermissiveOrInternalIp()) {
			return true;
		}
		return false;
	}

	@Override
	public Class<CaptchaRequired> getAnnotation() {
		return CaptchaRequired.class;
	}

	@Override
	public void preHandle(CaptchaRequired annotation, HandlerMethod handlerMethod) {
		if (handlerMethod.getMethod().getName().equals("miaoSha")) {
			if (!checkCaptchaStatusForBiz(handlerMethod))
				throw new CaptchaRequiredException();
			else
				return;
		}
		if (checkCaptchaStatus()) {
			return;
		}
		LOGGER.debug("验证码不正确!");
		throw new CaptchaRequiredException();
	}

	@Override
	public void afterHandle(CaptchaRequired annotation, HandlerMethod handlerMethod) {

	}

	/**
	 * 根据业务判断验证码是否正确
	 * 
	 * @param handlerMethod
	 * @return
	 */
	public boolean checkCaptchaStatusForBiz(HandlerMethod handlerMethod) throws CaptchaRequiredException {
		String reqCaptcha = RequestContext.getParameter("captcha");
		String token = CaptchaBuilder.SESSION_CAPTCHA.getValue();
		CaptchaBuilder.SESSION_CAPTCHA.remove();
		// RequestContext.getRemoteAddr() +
		// CookieSpec.build("ssoid").domain(".eascs.com")
		String userId = SESSION_USER_ID.getValue();
		long count_cap = frequencyService.get(RequestContext.getRemoteAddr() + userId, "loop", "pc");
		if (count_cap != 0) {
			frequencyService.frequency(RequestContext.getRemoteAddr() + userId, "loop", "pc", 60);
			return true;
		} else if (!StringUtils.isEmpty(reqCaptcha) && !StringUtils.isEmpty(token)) {
			if (reqCaptcha.equalsIgnoreCase(token)) {
				frequencyService.frequency(RequestContext.getRemoteAddr() + userId, "loop", "pc", 60);
				return true;
			}
		}
		LOGGER.debug("验证码不正确!");
		return false;
	}
}
